<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/classLogic.php");
include_once(constant('PROJECT_DIR')."/logic/contactLogic.php");
include_once(constant('PROJECT_DIR')."/logic/courseLogic.php");
$LIST_NUMBER_PER_PAGE=10;
//begin to read class
$cl = new classLogic();
$ul = new contactLogic();
if(!isset($_POST['action']))
{
     $action="";
}
else
{
     $action = $_POST['action'];
}
if($action == "")
{
		if(isset($_GET['action']))
		{
			$action=$_GET['action'];
		}
}
switch($action)
{
     case "addcontact":
         $id=$_POST['id'];
         $name=$_POST['name'];
         $booktime=$_POST['booktime'];
         $type=$_POST['type'];
         $class=$_POST['class'];
         $contact=array();
         $contact["id"]=$id;
         $contact["name"]=$name;
         $contact["booktime"]=$booktime;
         $contact["level"]=$type;
         $contact["passwd"]=md5($id);
         if($class>0)
         {
             $contact["classid"]=$class;
         }
         $contact["createtime"]=date("Y-m-d H:i:s");
         $ul->insert_contact($contact);
         break;
     case "deletecontact":
     			if(!isset($_POST['contact']))
     			{
     				break;
     				}
         $contacts=$_POST['contact'];
         for($i=0; $i<sizeof($contacts); $i++)
         {
             $ul->delete_contact($contacts[$i]);
         }
         break;
     case "modifycontact":
         $uid=$_POST['contactid'];
         $contact=array();
         $contact["id"]=$_POST["contactid"];
         $contact["name"]=$_POST["name"];
         $contact["booktime"]=$_POST["booktime"];
         $contact["level"]=$_POST["type"];
         $contact["email"]=$_POST["email"];
         $contact["phone"]=$_POST["phone"];
         $contact["info"]=$_POST["info"];
         $contact["classid"]=$_POST["class"];
         $ul->update_contact($contact, $uid);
         break;
     case "addUserCourse":
     			if(!isset($_POST['courseid']))
     			{
     					break;
     			}
         $contactCourse=$_POST["courseid"];
         $contactid=$_POST["contactid"];
         $booktime=$_POST["booktime"];
         $createtime=date("Y-m-d H:i:s");
         $existNumber=$ul->get_contact_course_number($contactid, $contactCourse);
         if($existNumber>0)
         {
         		$smarty->assign("addUserCourseNotice", "该用户与该课程对应关系已经存在");
         }
         else
         {
         		$ul->insert_contact_course($contactid, $contactCourse, $booktime, $createtime);
         		$smarty->assign("defaultBooktime", $booktime);
         }
         break;
     case "saveUserCourseBooktime":
     		$uid=$_POST['contactId'];
     		$cid=$_POST['courseId'];
     		$booktime=$_POST['booktime'];
     		if($booktime<0)
     		{
     			break;
     		}
     		$ul->update_contact_course_booktime($uid, $cid, $booktime);
     		break;
    case "removecontactcourse":
    		$uid=$_GET['id'];
     		$cid=$_GET['courseid'];
     		$ul->delete_contact_course($uid, $cid);
     		break;
}
if(isset($_GET['id']))
{
     //get the editting contact info
     $editUser = $ul->get_contact(array("id"=>$_GET["id"]));
     $smarty->assign("editUser", $editUser[0]);
     
     //读取所有课程
     $courseLg = new courseLogic();
     $courses= $courseLg->get_course("","order by id desc", "");
     
     
     //读取用户相关的课程
     $contactCourses=$ul->get_contact_courses($editUser[0]['id']);
     
     //去掉用户已经选择的课程
     $filterCourses=array();
     for($i=0; $i<count($courses); $i++)
     {
     		$contains=0;
     		for($j=0; $j<count($contactCourses); $j++)
     		{
     				if($contactCourses[$j]['cid'] == $courses[$i]['id'])
     				{
     						$contains=1;
     						break;
     				}
     		}
     		if($contains==0)
     		{
     			array_push($filterCourses, $courses[$i]);
     		}
     	}
     	$smarty->assign("courses", $filterCourses);
     	
     $courseId2Name=array();
     if(count($contactCourses)>0)
     {
     		for($i=0; $i< count($courses); $i++)
     		{
     				$courseId2Name[$courses[$i]['id']]=$courses[$i]['name'];
     			}
     	}
     for($i=0; $i < count($contactCourses); $i++)
     {
     		$contactCourses[$i]['courseName']=$courseId2Name[$contactCourses[$i]['cid']];
     }
     if(count($contactCourses)>0)
     {
     			$smarty->assign("contactCourses", $contactCourses);
     }
}


//handling the page index
$contactsNumber=$ul->get_contact_num();
$pageIndex=1;
$totalPageNumber=ceil($contactsNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("contactsNumber", $contactsNumber);
$allcontacts=$ul->get_contact('',"order by createtime desc", "limit $startItem,$ItemNumber");

//add the class info and the type
for($i=0; $i<sizeof($allcontacts); $i++)
{
     $searchClassConditionTmp=array("id"=>$allcontacts[$i]['classid']);
     $classTmp=$cl->get_class($searchClassConditionTmp,'','');
     $allcontacts[$i]['classid']= isset($classTmp[0]['name'])?$classTmp[0]['name']:"无班级";
}
$smarty->assign("contacts",$allcontacts);
//handling page Index end

//填充需要信息

$contactsnumber = $ul->get_contact_num();
$smarty->assign("contactnumber", $contactsnumber);

$allclasses = $cl->get_class('',"order by id desc", "");
$smarty->assign("classes", $allclasses);
$smarty->assign("menuSelected", "contact");
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("currentUrl","contact/contact.php");
$smarty->display("contact/contact.html");
?>
